--[[-------------------------------------------------------------

	Bellah of Stormrage - Jan 2009

	Frames for Lootlist addon
	Uses baseLib to get default UI components

---------------------------------------------------------------]]

local debug = false

local frameLib, oldMinor = LibStub:NewLibrary("LootListFrames", 1)

if not frameLib then
  return	-- already loaded and no upgrade necessary
end

frameLib.embeds = frameLib.embeds or {} -- table containing objects this lib is embedded in.
frameLib.commands = frameLib.commands or {} -- table containing commands registered
frameLib.weakcommands = frameLib.weakcommands or {} -- table containing self, command => func references for weak

function frameLib:SetDebug (value)
	debug = value
	if value then
		print("LootListFrames -- debug on")
	else
	    print ("LootListFrames - debug off")
	end
end

function frameLib:dbgPrint(text)
	if debug then
		print ("LootListFrames - " .. text)
	end
end

local baseLib = LibStub("FrameBaseLib")

--[[
frames for Lootlist (so we can dump the rather ghastly XML)

Layout:
---------------------------------------
| Instance list title                 |
|-------------------------------------|
| Instance / date/time              |^|
|                                   | |
|                                   | |
|                                   |X|
|                                   | |
|                                   | |
|                                   | |
|                                   |v|
|-------------------------------------|
|  [Clear selected] [ Clear All  ]    |
|-------------------------------------|

---------------------------------------
| Item list title                     |
|-------------------------------------|
| Item - Looter - need/greed/dkp    |^|
|                                   | |
|                                   | |
|                                   |X|
|                                   | |
|                                   | |
|                                   | |
|                                   |v|
|-------------------------------------|
|  [Clear selected] [ Clear All  ]    |
|-------------------------------------|

------------------------------ 
| Add new instance title     | 
------------------------------
| <Instance> <Date> <Time>   | 
|                            | 
| [ Add  ]      [ Cancel ]   | 
------------------------------ 

------------------------------
| Add new item title         |
------------------------------
| <Item>                     |
| <______>     [Add for DKP] |
| [ Add Need ] [ Add Greed ] |
|                            |
|                 [ Cancel ] | 
------------------------------
]]

-- GetInstanceListFrame - returns a frame with a 2-column list of nButtons deep with a scrollbar on the right
-- Optional: labels - table of title/button texts
local instFrame = nil
function frameLib:GetInstanceListFrame(...)
	local frameInfo, newFrame = ...
	
	if (instFrame and not newFrame) then return instFrame end
	
	-- for maintenance purposes: change these values display will follow automagically
	local fName = frameInfo.name or "LLInstListFrame"
	local fTitle = frameInfo.title or "Instance list"
	local bName= fName  .. "Btn"
	local nButtons = frameInfo.nButtons or 10
	local bHeight = 16
	local hdrGap = 5
	local fWidth = frameInfo.fWidth or 200
	local lColWidth = frameInfo.lColWidth or 0.6
	local fHeight = (nButtons + 3) * bHeight + 5*hdrGap
	local sWidth = 20
	local clrBWidth = fWidth - (2*hdrGap + sWidth)
	
	-- left and right button width divided over available space
	local bWidth = math.floor((fWidth - (sWidth + 2*hdrGap)) * lColWidth)
	local rbWidth = math.floor((fWidth - (sWidth + 2*hdrGap)) * (1-lColWidth))
	
	-- data list, view offset and selection index
	local instList = {}
	local offset = 0
	local sIndex = 1
	
	-- lets have a sound on clicks (and while we're at it)
	instFrame.buttonClicked = function(id)
		PlaySound("igMainMenuOptionCheckBoxOn")
		local index = offset + id
		
		-- TODO: Add selection highlighting
		-- TODO: Add data mangement
		
	end
	
	-- make sure that the left and right highlights sync on mouseEnter and mouseLeave
	-- TODO: Make this work with current selection highlight lock.
	local setHighlight = function (id, entering)
		local lBtn = getglobal(bName .. id .. "L")
		local rBtn = getglobal(bName .. id .. "R")
		if (entering) then
			lBtn:LockHighlight()
			rBtn:LockHighlight()
		else
			lBtn:UnlockHighlight()
			rBtn:UnlockHighlight()
		end
	end

-- The Frame
	-- baseLib:GetDefFrame(globalName, title, defW, defH [, parent [, bTex]])
	local instFrame = baseLib:GetDefFrame(fName, fTitle, fWidth, fHeight, nil, "Interface\\AddOns\\Lootlist\\textures\\otravi-semi-full-border")

-- The list
	-- create (nButtons) buttons & overlay highlights
	for i = 1, nButtons do
		-- create L/R buttons
		-- GetDefButton(globalName, defW, defH [,text [,id [,parent [,template [,nTex ,nBlend [,hTex, nBlend]]]]]])
		local lBtn = baseLib:GetDefButton(bName .. i .. "L", bWidth, bHeight, "", i, instFrame,
				"FriendsFrameIgnoreButtonTemplate", nil, nil, "Interface\\QuestFrame\\UI-QuestTitleHighlight", "ADD")
		local rBtn = baseLib:GetDefButton(bName .. i .. "R", bWidth, bHeight, "", i, instFrame,
				"FriendsFrameIgnoreButtonTemplate", nil, nil, "Interface\\QuestFrame\\UI-QuestTitleHighlight", "ADD")

		-- create L/R fontstrings
		-- GetDefLabel(globalName, parent [, text [, font]])
		local lBtnFS = baseLib:GetDefLabel(bName .. i .. "FSL", instFrame)
		local rBtnFS = baseLib:GetDefLabel(bName .. i .. "RSL", instFrame)
		
		lBtnFS:SetJustifyH("LEFT")
		lBtnFS:SetPoint("LEFT", lBtn, "LEFT")
		rBtnFS:SetJustifyH("LEFT")
		rBtnFS:SetPoint("LEFT", rBtn, "LEFT")

		-- button positioning
		if i == 1 then
			lBtn:SetPoint("TOPLEFT", instFrame.title, "BOTTOMLEFT", -2, -1 * hdrGap)
		else
			lBtn:SetPoint("TOPLEFT", getglobal(bName .. i - 1), "BOTTOMLEFT")
		end
		rBtn:SetPoint("TOPLEFT", lBtn, "TOPRIGHT")

		-- OnEnter / OnLeave scripts
		lBtn:SetScript ("OnEnter", function(self)
			setHighlight(self:GetID(), true)
		end)
		lBtn:SetScript ("OnLeave", function(self)
			setHighlight(self:GetID(), false)
		end)

		rBtn:SetScript ("OnEnter", function(self)
			setHighlight(self:GetID(), true)
		end)
		rBtn:SetScript ("OnLeave", function(self)
			setHighlight(self:GetID(), false)
		end)

		lBtn:SetScript ("OnClick", function(self)
				instFrame.buttonClicked(i)
			end)
		rBtn:SetScript ("OnClick", function(self)
				instFrame.buttonClicked(i)
			end)
	end

-- These functions to be overloaded from addon itself
	instFrame.clearInstance = function() return end
	instFrame.clearAll = function() return end
	
-- The clear current / clear all buttons
	-- GetDefButton(globalName, defW, defH [,text [,id [,parent [,template [,nTex ,nBlend [,hTex, nBlend]]]]]])
	local clearBtn = baselib:GetDefButton(nil, clrBWidth, bHeight, labels.clrText or "Clear Selected", nil, instFrame)
	clearBtn:SetPoint("BOTTOMLEFT", instFrame, "BOTTOMLEFT", hdrGap, 21)
	clearBtn:SetScript("OnClick", function()
			instFrame.clearInstance()
		end)

	local clearAllBtn = baselib:GetDefButton(nil, clrBWidth, bHeight, labels.clrAllText or "Clear All", nil, instFrame)
	clearAllBtn:SetPoint("LEFT", clearBtn, "RIGHT", hdrGap, 0)
	clearAllBtn:SetScript("OnClick", function()
			instFrame.clearAll()
		end)
end

local lootFrame = nil
function frameLib:GetItemListFrame(...)
	local labels, newCopy = ...
	
	if lootFrame then return lootFrame end
	
	if not labels then
		labels = {
			title = "Loot list"
		}
	end
	labels.name = "LLLootListFrame"

	local lootFrame = frameLib:GetInstanceListFrame(labels, true)
	
	return lootFrame
end

local addInstFrame = nil
function frameLib:GetAddInstanceFrame()

end

local addItemFrame = nil
function frameLib:GetAddItemFrame()

end
